<template>
	<view class="page-wrapper">
		<view class="btn-group">
			<view class="btn-wrapper">
				<button type="default" @click="authSetting">打开设置</button>
			</view>
			<view class="btn-wrapper">
				<button type="default" @click="getSetting">获取设置列表</button>
			</view>
			<view class="btn-wrapper">
				<button type="default" @click="getSAuthorize">定位信息</button>
			</view>
			<view class="btn-wrapper">
				<button type="default" @click="getUserInfo">用户信息</button>
			</view>
			<view class="btn-wrapper">
				<button type="default" @click="getuserLocationStill">后台定位</button>
			</view>
			<view class="btn-wrapper">
				<button type="default"  @click="getUserAddress">通信地址</button>
			</view>
		</view>
		
		
		<view class="getMessage">
			{{getMessage}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				getMessage:null
			};
		},
		
		methods:{

/* 
注意：获取用户地址信息权限，首先，如果在小程序其他页面已经获取信息，且用户已经同意，小程序会直接获取用户的地址信息。
	如果，在本页面之前，小程序已经问询过用户是否获取位置信息，且被用户拒绝，那么uni.authorize方法则直接运行失败方法。
 三月的雨
 轻轻的告诉你
 追梦人
 */			
			getSAuthorize:function(){
				console.log('查询是否获取地址权限')
				const that = this;
				uni.authorize({
					scope:"scope.userLocation",
					success: () => {
						uni.getLocation({
							success: (res) => {
								console.log('获取地址信息成功',res);
								that.getMessage = res;
							},
							fail: (err) => {
								console.log('获取用户信息失败',err);
							}
						})
					},
					fail: (err) => {
						console.log(err);
						console.log('用户拒绝获取地址信息权限')
						
					}
				})
			},
			
			// 小程序设置界面
			authSetting: function (){
				const that = this;
				console.log('打开小程序设置');
				uni.openSetting({
					success: (res) => {
						console.log('打开成功');
						console.log(res.AuthSetting);
						that.getMessage = res;
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			
			// get小程序设置
			getSetting: function(){
				const that = this;
				console.log("获取小程序当前设置");
				uni.getSetting({
					success: (res) => {
						console.log("成功获取用户设置")
						console.log('res',res);
						that.getMessage = res;
					},
					fail: (err) => {
						console.log("获取用户设置失败");
						console.log(err);
					}
				})
			},
			
			// 获取用户信息
			getUserInfo: function(){
				const that = this;
				console.log("获取用户信息--用户信息");
				uni.authorize({
					scope:"scope.userInfo",
					success: () => {
						console.log("用户尚未拒绝该权限");
						uni.getUserInfo({
							success: (res) => {
								that.getMessage = res;
								console.log("成功获取用户信息");
								console.log(res);
							},
							fail: (err) => {
								console.log("获取用户信息失败");
								console.log(err);
							}
						})
					},
					fail: () => {
						console.log("用户已经拒绝获取该权限");
					}
				})
			},
			
			// 后台获取用户位置信息
			getuserLocationStill: function(){
				const that = this;
				console.log("获取用户信息--地址信息");
				uni.authorize({
					scope:"scope.userLocationBackground",
					success: () => {
						console.log("用户尚未拒绝该权限");
						uni.userLocationBackground({
							success: (res) => {
								console.log("成功获取用户后台位置信息");
								console.log(res);
								that.getMessage = res;
							},
							fail: (err) => {
								console.log("获取用户后台位置信息失败");
								console.log(err);
							}
						})
					},
					fail: () => {
						console.log("用户已经拒绝获取该权限");
					}
				})
			},
			
			// 获取用户通信地址
			getUserAddress: function(){
				const that = this;
				console.log("获取用户信息--通信地址");
				uni.authorize({
					scope:"scope.address",
					success: () => {
						console.log("用户尚未拒绝该权限");
						uni.chooseAddress({
							success: (res) => {
								console.log("成功获取用户后台位置信息");
								console.log(res);
								that.getMessage = res;
							},
							fail: (err) => {
								console.log("获取用户后台位置信息失败");
								console.log(err);
							}
						})
					},
					fail: () => {
						console.log("用户已经拒绝获取该权限");
					}
				})
			},
		},
		onLoad: () => {
			console.log('页面onLoad');
		},
		onShow: () => {
			console.log('页面onShow');
		},
		onReady: () => {
			console.log('页面onReady');
		},
	}
</script>

<style lang="scss">
.btn-group{
			display: grid;
			grid-template-columns: 1fr 1fr;
			grid-column-gap: 10rpx;
			grid-row-gap: 10rpx;
			.btn-wrapper{
				font-size: 0.7rem;
				// padding: 10rpx;
			}
		}
</style>
